package model.wrap.user;

import java.util.ArrayList;
import java.util.List;

import controllers.routes;

import model.forview.RegistrationModelForView;

public class User
{
    protected models.User user;

    public static User getUser(String userName) throws UserException
    {
	User user = new User();
	List<models.User> userModel = models.User.getByUserName(userName);
	if (userModel.size() != 1)
	// TODO Добавить ексепшен вместо возвращения пустого значения!
	return null;
	user.setUserModel(userModel.get(0));
	return user;
    }

    public void setRandomPassword()
    {
	int min = 100000;
	int max = 999999;
	int newPass = min + (int) (Math.random() * ((max - min) + 1));
	this.setPassword(String.valueOf(newPass));
    }

    public String getFullName()
    {
	return getFirstName() + " " + getLastName();
    }

    public void save()
    {
	user.save();
    }

    public void update()
    {
	user.update();
    }

    public models.User getUserModel()
    {
	return user;
    }

    public void setUserModel(models.User user)
    {
	this.user = user;
    }

    public User()
    {
	if (user == null) user = new models.User();
    }

    public User(RegistrationModelForView form)
    {
	user = new models.User();
	setEmail(form.email);
	setPassword(form.password);
	setFirstName(form.firstName);
	setLastName(form.lastName);
	setUserType(user.READER);
    }

    public static ArrayList<User> getAllUsers()
    {
	List<models.User> users = models.User.getAllUsers();
	ArrayList<User> result = new ArrayList<User>();
	for (models.User user : users)
	{
	    User usr = new User();
	    usr.setUserModel(user);
	    result.add(usr);
	}
	return result;
    }

    public User(String userName) throws UserException
    {
	this.user = models.User.getUser(userName);
	if (this.user == null) throw UserException.errorCreaterNotFind();
    }

    public String getEmail()
    {
	return user.getUsername();
    }

    public String getPassword()
    {
	return user.getPassword();
    }

    public String getFirstName()
    {
	return user.getFirstname();
    }

    public String getLastName()
    {
	return user.getLastname();
    }

    public void setEmail(String email)
    {
	user.setUsername(email);
    }

    public void setPassword(String pass)
    {
	user.setPassword(pass);
    }

    public void setFirstName(String name)
    {
	user.setFirstname(name);
    }

    public void setLastName(String name)
    {
	user.setLastname(name);
    }

    public void setUserType(int type)
    {
	this.user.setType(type);
    }

    public int getUserType()
    {
	return this.user.getType();
    }

    public String getUserTypeToString()
    {
	if(isAdministrator())
	    return models.User.ADMINISTRATOR_TITLE;
	if (isSupervisor())
	    return models.User.SUPERVISOR_TITLE;
	if(isWriter())
	    return models.User.WRITER_TITLE;
	if(isReader())
	    return models.User.READER_TITLE;
	return models.User.GUEST_TITLE;
    }
    
    public boolean isAdministrator()
    {
	return models.User.ADMINISTRATOR == this.getUserType();
    }

    public boolean isSupervisor()
    {
	return models.User.SUPERVISOR == this.getUserType();
    }

    public boolean isWriter()
    {
	return models.User.WRITER == this.getUserType();
    }

    public boolean isReader()
    {
	return models.User.READER == this.getUserType();
    }

    public void setUserpic(byte[] pic)
    {
	this.user.setUserpic(pic);
    }

    public byte[] getUserpic()
    {
	return this.user.getUserpic();
    }
}
